package org.yaac.shared.property;

import static com.google.common.collect.Lists.newLinkedList;
import static com.google.common.collect.Lists.transform;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.yaac.shared.egql.ResultCell;
import org.yaac.shared.egql.ResultCell.ResultCellFactory;

import com.google.common.base.Function;
import com.google.common.base.Joiner;


/**
 * in appengine, elements in ListProperty are NOT necessary to be the same type
 * 
 * @author Max Zhu (thebbsky@gmail.com)
 *
 */
//TODO : implement ordering based on logics documented at http://code.google.com/appengine/docs/java/datastore/entities.html
public class ListPropertyInfo extends PropertyInfo {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private List<PropertyInfo> payload;
	
	public ListPropertyInfo() {
		payload = new LinkedList<PropertyInfo>();
	}
	
	public List<PropertyInfo> getPayload() {
		return payload;
	}

	public ListPropertyInfo add(PropertyInfo e) {
		payload.add(e);
		return this;
	}

	@Override
	public int compareTo(PropertyInfo o) {
		// List is not comparable
		return 0;
	}

	@Override
	public String asHtml() {
		List<String> asStrings = new ArrayList<String>();
		for (PropertyInfo info : payload) {
			asStrings.add(info.asHtml());
		}
		
		return "[" + Joiner.on(",<br/>").join(asStrings) + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((payload == null) ? 0 : payload.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ListPropertyInfo other = (ListPropertyInfo) obj;
		if (payload == null) {
			if (other.payload != null)
				return false;
		} else if (!payload.equals(other.payload))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "ListPropertyInfo [payload=" + payload + "]";
	}

	@Override
	public ResultCell populateResultCell(ResultCellFactory factory) {
		ResultCell cell = super.populateResultCell(factory);
		cell.setType(PropertyType.LIST);
		cell.setParent(null);
		cell.setPayload(null);
		cell.setChildren(new ArrayList<ResultCell>(payload.size()));
		for (PropertyInfo child : payload) {
			cell.getChildren().add(child.populateResultCell(factory));
		}
		return cell;
	}

	@Override
	public String asExpression(final FilePlaceHolder placeHolder) {
		List<String> expressions = transform(this.payload, 
				new Function<PropertyInfo, String>(){
			@Override
			public String apply(PropertyInfo input) {
				return input.asExpression(placeHolder);
			}
		});
		
		return "list(" + Joiner.on(",").join(expressions) + ")";
	}
	
	@Override
	public List<String> downloadPaths() {
		List<String> result = newLinkedList();
		
		for (PropertyInfo child : this.payload) {
			result.addAll(child.downloadPaths());
		}
		
		return result;
	}
}
